﻿Public Class UserInputForm
    Public userInfo As User = New User
    Dim compForm As UserInputCompleteForm
    Private Sub InputForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ユーザ情報をコントロールで表示
        txtUserID.Text = userInfo.userID
        txtUserName.Text = userInfo.userName
        txtPwd.Text = userInfo.password
        txtRePwd.Text = userInfo.repassword
        Select Case Me.userInfo.userType
            Case "A"
                rbUserTypeA.Checked = True
            Case "B"
                rbUserTypeB.Checked = True
            Case "C"
                rbUserTypeC.Checked = True
        End Select
        '表示タイプ設定（登録、更新、削除）
        Select Case userInfo.dispFlg
            Case "I"
                btnSubmit.Text = "登録"
            Case "U"
                InterfaceCommon.readonlyTextBox(txtUserID)
                btnSubmit.Text = "更新"
            Case "D"
                InterfaceCommon.readonlyTextBox(txtUserID)
                InterfaceCommon.readonlyTextBox(txtUserName)
                InterfaceCommon.readonlyTextBox(txtPwd)
                InterfaceCommon.readonlyTextBox(txtRePwd)
                InterfaceCommon.readonlyRadioButton(rbUserTypeA)
                InterfaceCommon.readonlyRadioButton(rbUserTypeB)
                InterfaceCommon.readonlyRadioButton(rbUserTypeC)
                btnSubmit.Text = "削除"
        End Select
    End Sub
    'キャンセルボタン処理
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
    'サーブミット処理
    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim ans = MessageCommon.showConfirm(MessageCommon.getMessage(userInfo.getActionName, My.Resources.K_ACTION_CONFIRM))
        If ans = Windows.Forms.DialogResult.OK Then
            Dim errorsMsg As String
            'ユーザ情報をFormに設定
            userInfo.userID = txtUserID.Text
            userInfo.password = txtPwd.Text
            userInfo.repassword = txtRePwd.Text
            userInfo.userName = txtUserName.Text
            If rbUserTypeA.Checked = True Then
                Me.userInfo.userType = "A"
            ElseIf rbUserTypeB.Checked = True Then
                Me.userInfo.userType = "B"
            ElseIf rbUserTypeC.Checked = True Then
                Me.userInfo.userType = "C"
            End If
            'バリデーション実行
            errorsMsg = Me.userInfo.validate(False)
            If String.Equals(errorsMsg, "") = False Then '入力エラー有り
                MessageCommon.showError(errorsMsg)
                Exit Sub
            End If
            'DB登録処理
            Dim dao = New M_UserDao
            Select Case userInfo.dispFlg
                Case "I"
                    'ユーザID保存チェック
                    errorsMsg = dao.checkExistedUser(Me.userInfo.userID)
                    If String.Equals(errorsMsg, "") = False Then '入力エラー有り
                        MessageCommon.showError(errorsMsg)
                        Exit Sub
                    End If
                    errorsMsg = dao.insertUser(Me.userInfo, My.Settings.CurrentUserID)
                Case "U"
                    errorsMsg = dao.updateUser(Me.userInfo, My.Settings.CurrentUserID)
                Case "D"
                    errorsMsg = dao.deleteUser(Me.userInfo.userID, My.Settings.CurrentUserID)
            End Select
            If String.Equals(errorsMsg, "") = False Then 'DB登録エラー有り
                MessageCommon.showError(errorsMsg)
                Me.Close()
                Exit Sub
            End If
            'DB登録正常
            compForm = New UserInputCompleteForm
            compForm.userInfo.dispFlg = Me.userInfo.dispFlg
            compForm.userInfo.userID = Me.txtUserID.Text
            compForm.userInfo.userName = Me.txtUserName.Text
            If rbUserTypeA.Checked = True Then
                compForm.userInfo.userType = "管理"
            ElseIf rbUserTypeB.Checked = True Then
                compForm.userInfo.userType = "購買"
            ElseIf rbUserTypeC.Checked = True Then
                compForm.userInfo.userType = "開発"
            End If
            '完了画面表示し、当画面を閉じる
            compForm.ShowDialog()
            Me.Close()
        End If
        
    End Sub
End Class